import cherrypy 
import sqlite3
 
def connect(thread_index): 
    # Create a connection and store it in the current thread 
    cherrypy.thread_data.db = sqlite3.connect('sample.db') 
 
# Tell CherryPy to call "connect" for each thread, when it starts up 
cherrypy.engine.subscribe('start_thread', connect)

 
class Root: 
    def index(self): 
        # Sample page that displays the number of records in "table" 
        # Open a cursor, using the DB connection for the current thread 
        c = cherrypy.thread_data.db.cursor() 
        c.execute('select * from item') 
        
        yield "<table border=1>\n"
        for row in c:
	    yield "<tr>"
	    for col in row:
		yield "<td>%s</td>" % col
	    yield "</tr>\n"
	yield "</table>"
        
    index.exposed = True 
 
cherrypy.quickstart(Root())
